package com.china.zhuoyue.ui.activity.login

import android.view.View
import com.china.zhuoyue.api.login.launchUILogin
import com.china.zhuoyue.databinding.ActivityForgetPasswordBinding
import com.china.zhuoyue.ui.api.ApiObserver
import com.china.zhuoyue.ui.base.BaseActivity
import com.china.zhuoyue.ui.ext.value
import com.china.zhuoyue.ui.utils.variableView

class ForgetPasswordActivity : BaseActivity<ActivityForgetPasswordBinding>() {

    override fun initView() {
        loadView {
            bindViewClick(buttonForgetPassword, tbVcode)
            variableView {
                addTextView(etPhone)
                addTextView(etNewPassword)
                addTextView(etVcode)
                addTextView(etDoublePassword)
                addOnValidationInterceptListener(::intercept)
                addOnValidationResultListener(buttonForgetPassword)
            }
        }
    }

    private fun intercept(view: View): Boolean? = when (view) {
        vb.etPhone -> vb.tbVcode.enable(vb.etPhone.length() == 11)
        vb.etVcode -> vb.etVcode.length() == 6
        vb.etNewPassword -> vb.etNewPassword.length() in 6..16
        vb.etDoublePassword -> vb.etDoublePassword.length() in 6..16
        else -> null
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.tbVcode -> {
                launchUILogin({
                    sendSms(vb.etPhone.value, "001")
                }, ApiObserver(this) {
                    vb.tbVcode.start()
                })
            }
            vb.buttonForgetPassword -> login()
        }
    }

    private fun login() {
//        launchUI({
//            forgetPassword(
//                vb.editPhone.value,
//                vb.editPaw.value,
//                vb.etVcode.value,
//            ).toData()
//        }, ApiObserver(this) {
//            setResult(RESULT_OK)
//            finish()
//        })
    }
}